package com.laomuji.android.mvvm.utils

import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.util.TypedValue
import android.view.WindowInsets
import android.view.WindowManager
import androidx.annotation.ColorRes
import androidx.annotation.StringRes


object ResourceUtil {
    /**
     * dp转px
     */
    fun dpToPx(dp: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp.toFloat(),
            Resources.getSystem().displayMetrics
        )
    }

    /**
     * sp转px
     */
    fun spToPx(sp: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            sp.toFloat(),
            Resources.getSystem().displayMetrics
        )
    }

    private fun getContextResources():Resources{
        return PackageUtil.getApplicationContext().resources
    }

    fun getString(@StringRes resId:Int):String{
        return getContextResources().getString(resId)
    }

    fun getColor(@ColorRes resId:Int):Int{
        //api 23 以下只能这样使用
        @Suppress("DEPRECATION")
        return getContextResources().getColor(resId)
    }

    /**
     * 获取状态栏高度
     * @android:dimen/status_bar_height
     */
    fun getStatusBarHeight():Int{
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowManager = PackageUtil.getApplicationContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val windowInsets = windowManager.currentWindowMetrics.windowInsets
            return windowInsets.getInsets(WindowInsets.Type.statusBars()).top
        } else {
            val resources = getContextResources()
            val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
            return if (resourceId > 0) {
                resources.getDimensionPixelSize(resourceId)
            }else {
                0
            }
        }
    }

    /**
     * 获取导航栏高度
     * @android:dimen/navigation_bar_height
     */
    fun getNavBarHeight():Int {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val windowManager = PackageUtil.getApplicationContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val windowInsets = windowManager.currentWindowMetrics.windowInsets
            val insets = windowInsets.getInsets(WindowInsets.Type.navigationBars())
            val systemInsets = windowInsets.getInsets(WindowInsets.Type.systemBars())

            return when {
                insets.bottom > 0 && systemInsets.bottom == insets.bottom -> insets.bottom
                insets.left > 0 -> insets.left
                insets.right > 0 -> insets.right
                else -> 0
            }
        }else{
            val resources = getContextResources()
            val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            return if (resourceId > 0) {
                resources.getDimensionPixelSize(resourceId)
            } else {
                0
            }
        }
    }
}